//
//  StoreAPI.m
//  RainbowFM
//
//  Created by Kratos on 14/12/19.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "StoreAPI.h"
#import "AFAppDotNetAPIClient.h"


@implementation StoreAPI

-(void)fetchGoodsFirstCatalogWithuserId:(NSString *)userId
                               userCode:(NSString *)userCode
                                 result:(void (^)(NSArray *catalog, NSString *msg))res{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }else{
        [params setObject:@"" forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_first_catalog_req",
                                                   @"callType":@"002",
                                                     @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
        
    }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


-(void)fetchGoodsSecondCatalogWithuserId:(NSString *)userId
                               userCode:(NSString *)userCode
                          firstCatalogId:(NSString *)firstCatalogId
                                 result:(void (^)(NSArray *catalog, NSString *msg))res{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }else{
        [params setObject:@"" forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    if (firstCatalogId) {
        [params setObject:firstCatalogId forKey:@"firstCatalogId"];
    }
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_second_catalog_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

-(void)fetchGoodsCatalogWithuserId:(NSString *)userId
                               userCode:(NSString *)userCode
                                 result:(void (^)(NSArray *catalogList, NSString *msg))res{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }else{
        [params setObject:@"" forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_catalog_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}


-(void)searchGoodsWithuserId:(NSString *)userId
                    userCode:(NSString *)userCode
              firstCatalogId:(NSString *)firstCatalogId
             secondCatalogId:(NSString *)secondCatalogId
                         key:(NSString *)key
                    sortFlag:(NSString *)sortFlag
                 currentPage:(NSString *)currentPage
                    pageSize:(NSString *)pageSize
                      result:(void (^)(NSArray *goodsList, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    if (firstCatalogId) {
        [params setObject:firstCatalogId forKey:@"firstCatalogId"];
    }else{
        [params setObject:@"" forKey:@"firstCatalogId"];
    }
    
    if (secondCatalogId) {
        [params setObject:secondCatalogId forKey:@"secondCatalogId"];
    }else{
        [params setObject:@"" forKey:@"secondCatalogId"];
    }
    
    if (key) {
        [params setObject:key forKey:@"key"];
    }else{
        [params setObject:@"" forKey:@"key"];
    }
    
    if (sortFlag) {
        [params setObject:sortFlag forKey:@"sortFlag"];
    }else{
        [params setObject:@"" forKey:@"sortFlag"];
    }
    
    if (!currentPage || !pageSize) {
        res(nil,@"缺少 当前页 和 每页条数");
        return;
    }else
    {
        [params setObject:currentPage forKey:@"currentPage"];
        [params setObject:pageSize forKey:@"pageSize"];
    }
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_product_list_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}


-(void)fetchGoodsDetailWithuserId:(NSString *)userId
                         userCode:(NSString *)userCode
                        productId:(NSString *)productId
                           result:(void(^)(NSArray *goodsDetail, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }

    
    if (!productId) {
        res(nil,@"缺少 商品 ID");
        return;
    }
    
    [params setObject:productId forKey:@"productId"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_product_detail_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) { 
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}

-(void)fetchGoodsStockWithuserId:(NSString *)userId
                        userCode:(NSString *)userCode
                       productId:(NSString *)productId
                            flag:(NSString *)flag
                          result:(void(^)(NSArray *goodsStock, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }else{
        [params setObject:@"" forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }else{
        [params setObject:@"" forKey:@"userCode"];
    }
    
    if (!productId || !flag) {
        res(nil,@"缺少 商品 ID 或 标志");
        return;
    }
    
    [params setObject:flag forKey:@"flag"];
    [params setObject:productId forKey:@"productId"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_stock_num_change_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

-(void)fetchMerchantTypeListWithuserId:(NSString *)userId
                              userCode:(NSString *)userCode
                                result:(void(^)(NSArray *typeList, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_merchant_type_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}

-(void)fetchMerchantsListWithuserId:(NSString *)userId
                           userCode:(NSString *)userCode
                              colId:(NSString *)colId
                                key:(NSString *)key
                             typeId:(NSString *)typeId
                        currentPage:(NSString *)currentPage
                           pageSize:(NSString *)pageSize
                             result:(void(^)(NSArray *list, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }
    
    if (colId) {
        [params setObject:colId forKey:@"colId"];
    }
    
    
    if (key) {
        [params setObject:key forKey:@"key"];
    }
    
    if (typeId) {
        [params setObject:typeId forKey:@"typeId"];
    }
    
    if (!currentPage || !pageSize) {
        res(nil,@"缺少 当前页 和 每页条数");
        return;
    }else
    {
        [params setObject:currentPage forKey:@"currentPage"];
        [params setObject:pageSize forKey:@"pageSize"];
    }
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_merchant_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

-(void)searchMerchantDetailWithuserId:(NSString *)userId
                       userCode:(NSString *)userCode
                     merchantId:(NSString *)merchantId
                         result:(void(^)(NSDictionary *merchant, NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (userId) {
        [params setObject:userId forKey:@"userId"];
    }
    
    if (userCode) {
        [params setObject:userCode forKey:@"userCode"];
    }
    
    if (!merchantId) {
        res(nil,@"缺少 商家ID");
        return;
    }
    
    [params setObject:merchantId forKey:@"merchantId"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:MALL_BASE_URL
                                   parameters:@{@"serviceName":@"mall_merchant_detail_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res([responseObject[@"result"][@"list"] firstObject],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}

@end
